﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Threading;

namespace PdfStudio
{

    class TataNotifyIcon
    {
        #region Fields
        Queue<BalloonTip> balloonTipQueue = new Queue<BalloonTip>();
        NotifyIcon notifyIcon;
        WorkState state = WorkState.Idle;
        #endregion

        #region Ctor
        public TataNotifyIcon(IContainer components)
        {
            notifyIcon = new NotifyIcon(components);
            notifyIcon.BalloonTipClosed += new EventHandler(NotifyIcon_BalloonTipClosed);
        }
        #endregion

        #region Properties
        public string Text
        {
            get
            {
                return notifyIcon.Text;
            }
            set
            {
                notifyIcon.Text = value;
            }
        }

        public ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return notifyIcon.ContextMenuStrip;
            }
            set
            {
                notifyIcon.ContextMenuStrip = value;
            }
        }

        public Icon Icon
        {
            get
            {
                return notifyIcon.Icon;
            }
            set
            {
                notifyIcon.Icon = value;
            }
        }

        public bool Visible
        {
            get
            {
                return notifyIcon.Visible;
            }
            set
            {
                notifyIcon.Visible = value;
            }
        }

        public MouseEventHandler MouseDoubleClick
        {
            set
            {
                notifyIcon.MouseDoubleClick += value;
            }
        }

        public void ShowBalloonTip(int timeout, string title, string text, ToolTipIcon icon)
        {
            BalloonTip balloonTip = new BalloonTip();
            balloonTip.Title = title;
            balloonTip.Text = text;
            balloonTip.Icon = icon;
            balloonTip.Timeout = timeout;
            if (state == WorkState.Idle)
            {
                ShowBalloonTip(balloonTip);
            }
            else
            {
                balloonTipQueue.Enqueue(balloonTip);
            }
        }

        private void ShowBalloonTip(BalloonTip balloonTip)
        {
            notifyIcon.ShowBalloonTip(balloonTip.Timeout, balloonTip.Title,
                                                            balloonTip.Text, balloonTip.Icon);

            state = WorkState.Busy;
        }

        public void NotifyIcon_BalloonTipClosed(object sender, EventArgs e)
        {
            if (balloonTipQueue.Count > 0)
            {
                ShowBalloonTip(balloonTipQueue.Dequeue());
            }

            state = WorkState.Idle;
        }


        #endregion
    }
}
